<?php

/**
 * @version     1.0.0
 * @package     com_works
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Quang <quang4it@yahoo.com> - http://quang.com
 */
// No direct access
defined('_JEXEC') or die;

/**
 * @param	array	A named array
 * @return	array
 */
function WorksBuildRoute(&$query) {
    $segments = array();

    $app = new App();
    if (isset($query['view'])) {
        unset($query['view']);
    }
//    echo "<pre>";
//var_dump($query['cat_id']);
//echo "</pre>";
    if (isset($query['cat_id'])) {
        $cat = $app->getWorkCategory($query['cat_id']);
        $segments[] = $cat->alias;
        unset($query['cat_id']);
    }
    if (isset($query['id'])) {
        $p = $app->getWork($query['id']);
        $segments[] = $p->alias;
        unset($query['id']);
    }

    return $segments;
}

/**
 * @param	array	A named array
 * @param	array
 *
 * Formats:
 *
 * index.php?/works/task/id/Itemid
 *
 * index.php?/works/id/Itemid
 */
function WorksParseRoute($segments) {
    $vars = array();
    $app = new App();
    // view is always the first element of the array
    $count = count($segments);
    $vars['view'] = 'works';
    if ($count == 2) {
        if (is_numeric($segments[1])) {
            $vars['id'] = $segments[1];
        } else {
            $p = $app->getWork(0, $segments[1]);
            if (is_object($p) && $p && isset($p->id)) {
                $vars['id'] = $p->id;
            }
        }
        $vars['view'] = 'work';
    } elseif ($count == 1 && $segments[0]) {
        $cat = $app->getWorkCategory(0, $segments[0]);
        $vars['cat_id'] = $cat[0]->id ? $cat[0]->id : 0;
        $vars['view'] = 'works';
    }

    return $vars;
}
